#include "../inc/Camera.h"

Camera::Camera() :
  ar(4.0/3.0)
, fov(45.0)
, fardist(-1.0)
, neardist(1.0)
, cPos(0,10,10)
, cLookAt(0,0,0)
, upVec(0,1,0)
{
}

Camera::Camera( CoordF p ) :
  ar(4.0/3.0)
, fov(45.0)
, fardist(-1.0)
, neardist(1.0)
, cLookAt(0,0,0)
, upVec(0,1,0)
{
  cPos = p;
}

Camera::Camera( CoordF p, CoordF l ) :
  ar(4.0/3.0)
, fov(45.0)
, fardist(-1.0)
, neardist(1.0)
, upVec(0,1,0)
{
  cPos = p;
  cLookAt = l;
}

void Camera::setupCamera()
{
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  
  gluPerspective(fov, ar, neardist, fardist);
  gluLookAt(cPos.x, cPos.y, cPos.z, cLookAt.x, cLookAt.y, cLookAt.z, upVec.x, upVec.y, upVec.z);
  
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

}

void Camera::displace(GLfloat v)
{
   CoordF t = getLookAt();
   t-=cPos;
   t.normalize();
   cPos += t * v;
}

void Camera::strafe(GLfloat v)
{
  CoordF t1 = getLookAt() - cPos;
  t1.normalize();
  CoordF t2(t1,upVec);
  t2.normalize();
  cPos += t2 * v;
  cLookAt += t2 * v;
}

void Camera::raise(GLfloat v)
{
     cPos += upVec * v;
     cLookAt += upVec * v;
}

void Camera::rotate ( GLfloat ang )
{
     roll += ang;
     
     // Corrige amplitude de valores
     while (roll < -360.0) roll += 360.0;
     while (roll > 360.0) roll -= 360.0;
     
     setRotation(roll);

}
